package cn.itrip.search.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.vo.hotel.SearchHotCityVO;
import cn.itrip.beans.vo.hotel.SearchHotelVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.Page;
import cn.itrip.search.beans.Hotel;
import cn.itrip.search.service.SearchService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
@Controller
@RequestMapping("/api/hotellist")
public class SearchController {
    @Resource(name = "searchService")
    private SearchService searchService;
    @RequestMapping(value = "/searchItripHotelListByHotCity",method = RequestMethod.POST)
    @ResponseBody
    public Dto searchItripHoteListByHotCity(@RequestBody SearchHotCityVO vo){
        if (EmptyUtils.isEmpty(vo.getCityId())) {
            return  DtoUtil.returnFail("城市Id不能为空","20004");
        }
        try {
            List<Hotel> list= searchService.getAllHotelList(vo);
            if(list.size()>0){
                for(int i=0;i<list.size();i++){
                    System.out.println(list.get(i).getAddress());
                }
            }
            return DtoUtil.returnSuccess("成功!",list);
        } catch (Exception e) {
            e.printStackTrace();
            return  DtoUtil.returnFail(e.getMessage(),"20003");
        }
    }


    @RequestMapping(value = "/searchItripHotelPage",method = RequestMethod.POST)
    @ResponseBody
    public  Dto  searchItripHotelPage(@RequestBody SearchHotelVO vo){
        if(EmptyUtils.isEmpty(vo.getDestination())){
            return  DtoUtil.returnFail("目的地不能为空","20004");
        }

        try {
            Integer pageSize2 =5;
            if(vo.getPageSize()!=null){
                pageSize2 = vo.getPageSize();
            }
            Integer pageNo2 =1;
            if(vo.getPageNo()!=null){
                pageNo2 = vo.getPageNo();
            }
            Page<Hotel> page=searchService.getLimitHotelList(vo,pageNo2,pageSize2);
            if(page.getPageSize()>0){
                for(int i=0;i<page.getRows().size();i++){
                    System.out.println(page.getRows().get(i).getAddress());
                }
            }
            return DtoUtil.returnSuccess("成功!",page);
        } catch (Exception e) {
            e.printStackTrace();
            return  DtoUtil.returnFail(e.getMessage(),"20001");
        }
    }




}
